<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="showDialog=true">新增角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="roleList">
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色" prop="name">
          <template v-slot="{ row }">
            <el-input v-if="row.isShow" v-model="row.edit.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="启用" prop="state">
          <template v-slot="{ row }">
            <el-switch v-if="row.isShow" v-model="row.edit.state" size="mini" :active-value="1" :inactive-value="0" />
            <span v-else>{{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述" prop="description">
          <template v-slot="{ row }">
            <el-input v-if="row.isShow" v-model="row.edit.description" type="textarea" size="mini" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <template v-if="row.isShow">
              <el-button type="primary" size="mini" @click="editRole(row)">确定</el-button>
              <el-button size="mini" @click="row.isShow=false">取消</el-button>
            </template>
            <template v-else>
              <el-button size="mini" type="text" @click.native="privileges(row.id)">分配权限</el-button>
              <el-button size="mini" type="text" @click="editShow(row)">编辑</el-button>
              <el-button size="mini" type="text" @click="delRole(row.id)">删除</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination :background="true" layout="sizes, prev, pager, next" :total="total" :page-size="pagesize" :current-page.sync="page" :page-sizes="[5, 10, 20, 30]" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
      </el-row>
    </div>

    <!-- 弹框 -->
    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog" @close="close">
      <!-- 表单内容 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model.trim="form.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch v-model="form.state" size="mini" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model.trim="form.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="hSave">确定</el-button>
              <el-button size="mini" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>

    <promission ref="proRef" :is-show.sync="isShow" :role-id="roleId" />
  </div>
</template>
<script>
import { getPageRole, addRole, delRole, editRole } from '@/api/role'

import promission from './components/promission.vue'
export default {
  name: 'Role',
  components: { promission },
  data() {
    return {
      page: 1,
      pagesize: 10,
      roleList: [],
      total: 0,
      showDialog: false,
      form: {
        name: '',
        description: '',
        state: 0
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      roleId: null,
      isShow: false
    }
  },
  created() {
    this.pageRole()
  },
  methods: {
    async pageRole() {
      const res = await getPageRole({ page: this.page, pagesize: this.pagesize })
      res.data.rows.forEach(i => {
        i.isShow = false
        i.edit = {
          name: i.name,
          description: i.description,
          state: i.state
        }
      }
      )
      this.roleList = res.data.rows
      this.total = res.data.total
      this.roleList.forEach(i => {
        // this.$set(i, 'isShow', false)
        // this.$set(i, 'edit', {
        //   name: i.name,
        //   description: i.description,
        //   state: i.state
        // })
      })
    },
    close() {
      this.showDialog = false
      this.$refs.form.resetFields()
    },
    hSave() {
      this.$refs.form.validate(valid => {
        if (valid) this.addRole()
      })
    },
    async addRole() {
      const res = await addRole(this.form)
      this.$message.success(res.message)
      this.close()
      this.page = Math.ceil(this.total++ / this.pagesize)
      this.pageRole()
    },
    handleCurrentChange(page) {
      this.page = page
      this.pageRole()
    },
    handleSizeChange(e) {
      this.pagesize = e
      this.page = 1
      this.pageRole()
    },
    delRole(id) {
      this.$confirm('确定要删除吗?').then(async() => {
        const res = await delRole(id)
        this.$message.success(res.message)
        if (this.roleList.length === 1) {
          if (this.page > 1) this.page--
        }
        this.pageRole()
      }).catch(() => {})
    },
    async editRole(row) {
      if (row.edit.name && row.edit.description) {
        const res = await editRole({ ...row.edit, id: row.id })
        this.$message.success(res.message)
        Object.assign(row, { ...row.edit, isShow: false })
        this.pageRole()
      } else {
        this.$message.error('角色名称或角色描述不能为空')
      }
    },
    editShow(row) {
      row.isShow = true
      row.edit.name = row.name
      row.edit.description = row.description
      row.edit.state = row.state
    },
    privileges(id) {
      this.isShow = true
      this.roleId = id
      this.$nextTick(() => {
        this.$refs.proRef.getRoleDetail()
      })
    }

  }
}
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
